//
//  WSVipPayController.m
//  CharonTVProject
//
//  Created by karong on 2023/8/11.
//

#import "WSVipPayController.h"
#import "WSSubscribeCell.h"
#import "WSPurchaseRequest.h"
#import "AppDelegate.h"
#import "WSPurchaseManager.h"
#import "WSGoods.h"
#import "UIButton+ImageAlign.h"

@interface WSVipPayController ()<WSPurchaseManagerDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainViewBottom;
@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (nonatomic, strong) NSArray * dataArray;
@property (weak, nonatomic) IBOutlet UIButton *payBtn;
@property (nonatomic, assign) NSInteger selectIndex;

@end

@implementation WSVipPayController
@synthesize dataArray = _dataArray;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadGoodsListRequest];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.mainViewBottom.constant = 0;
        [self.view uploadLayout];
    });
}


- (void)loadGoodsListRequest {
    WeakSelf
    [WSToast loading];
    [WSPurchaseRequest loadcharonTV_GoodsListWithGoods_type:2 success:^(NSArray *array) {
        weakSelf.dataArray = array;
        [WSPurchaseManager shared].delegate = weakSelf;
        [WSPurchaseManager shared].products = array;
    } failed:^(NSURLResponse * _Nonnull response, NSError * _Nonnull error, id  _Nonnull responseObject) {
        
    }];
}

- (void)payGoods:(WSGoods *)goods {
    if (goods.SKProduct) {
        if (appUserCenter.isLogin) {
            [WSPurchaseManager shared].delegate = self;
            [[WSPurchaseManager shared] createOrderWithProduct:goods];
        }
        else {
            [((AppDelegate*)([UIApplication sharedApplication].delegate)) initMainPage];
        }
    }
}

- (IBAction)payAction:(id)sender {
    [self payGoods:self.dataArray[self.selectIndex]];
}
#pragma mark - WSPurchaseManagerDelegate
- (void)didAppleReceiveResponse {
    [WSToast dismiss];
    [self.listView reloadData];
}

- (void)purchaseManager:(WSPurchaseManager *)manager updateStatus:(WSPurchaseStatus)status object:(id)object {
    switch (status) {
        case WSPurchaseStatusSuccess:
        {
            
        }
            break;
        case WSPurchaseStatusCancel:
            [WSToast simple:@"支付失敗"];
            break;
        default:
            break;
    }
}

#pragma mark UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSSubscribeCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSSubscribeCell" forIndexPath:indexPath];
    [cell setDataWithObject:self.dataArray[indexPath.row] select:self.selectIndex == indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = 98;
    size.height = 98;
    return size;
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.selectIndex = indexPath.row;
    [self.listView reloadData];
}

@end
